
#import "UIButton+CountDown.h"
#import <objc/runtime.h>


static char *kCOUNT_DOWN_TIMER = "count_down_timer";

@implementation UIButton (CountDown)

- (void)setCount_down_timer:(dispatch_source_t)count_down_timer {
    objc_setAssociatedObject(self, kCOUNT_DOWN_TIMER, count_down_timer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (dispatch_source_t)count_down_timer {
    return objc_getAssociatedObject(self, kCOUNT_DOWN_TIMER);
}

- (void)cancelCountdown {
    if (self.count_down_timer) {
        dispatch_source_cancel(self.count_down_timer);
    }
}

- (void)startTime:(NSInteger)timeout waitBlock:(void(^)(NSInteger remainTime))waitBlock finishBlock:(void(^)())finishBlock;
{
    __block NSInteger timeOut = timeout;
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    self.count_down_timer = _timer;
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    __weak typeof(self) weakSelf = self;
    dispatch_source_set_event_handler(_timer, ^{
        
        if(timeOut <= 0)
        { //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                if (finishBlock)
                {
                    finishBlock();
                }
                weakSelf.userInteractionEnabled = YES;
            });
        }
        else
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (waitBlock)
                {
                    waitBlock(timeOut);
                }
                weakSelf.userInteractionEnabled = NO;
            });
            timeOut--;
        }
    });
    dispatch_resume(_timer);
}


-(void)statrWithTime:(NSInteger)timeLine title:(NSString *)title countDownTitle:(NSString *)subString mainColor:(UIColor *)mColor countColor:(UIColor *)color
{
    
    //倒计时时间
    __block NSInteger timeOut = timeLine;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    self.count_down_timer = _timer;
    //没秒执行一次
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{
        
        //倒计时结束 关闭
        if (timeOut <= 0) {
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //
                [self setTitleColor:mColor forState:UIControlStateNormal];
                
                [self setTitle:title forState:UIControlStateNormal];
                self.userInteractionEnabled = YES;
            });
        } else {
            
            int allTime = (int)timeLine + 1;
            int seconds = timeOut % allTime;
            NSString *timeStr = [NSString stringWithFormat:@"%0.2d",seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
                [self setTitle:[NSString stringWithFormat:@"%@%@",timeStr,subString] forState:UIControlStateNormal];
                [self setTitleColor:color forState:UIControlStateNormal];
                self.userInteractionEnabled = NO;
                
            });
            
            timeOut --;
            
        }
        
        
    });
    dispatch_resume(_timer);
    
}

@end
